<?php
session_start();
//Requerimientos------
require '../../../MApache/weblinx/weblinx.php';
require '../Classes/Commandsreader.php';
require '../Forms/FrmUploadScriptJS.php';

//--------------------
if (isset ($_FILES['uploadScriptJS'])){
    
    $errors = array();
    $allowed_ext =  array('js');

    $file_name = $_FILES['uploadScriptJS']['name'];
    $file_ext = strtolower(end(explode('.', $file_name)));    
    $file_size = $_FILES['uploadScriptJS']['size'];
    $file_tmp = $_FILES['uploadScriptJS']['tmp_name'];

    if(in_array($file_ext, $allowed_ext) === false){

        $errors[] = 'la extension no es valida';
    }


    if($file_size > 2097152){

        $errors[] = 'el arhivo pasa el peso permitido';
    }

    if(empty ($errors)){

        if(move_uploaded_file($file_tmp, '../Data/'.$file_name)){
            echo 'arhivo cargado';
        }
    }else{
        foreach ($errors as $error){
            echo $error.'<br />';
        }
    }

    //Actualización de 02UpdateStack.js
    $commandReader=new CommandsReader();
    $commandReader->buildUpdateStack();

}
else if(isset($_POST['delete'])){
    unlink("../Data/".$_POST['ScriptsJSList']);
    //Generar array de archivos JS
    $jss=scandir("../Data");
    foreach ($jss as $js){
        if(substr($js, 0,1)!="." &&substr($js, 0,1)!="0"){
            $jssFinal[]=$js;
        }
    }
    
    //Actualización de 02UpdateStack.js
    $commandReader=new CommandsReader();
    $commandReader->buildUpdateStack();
    
    $frmUploadJS=new FrmUploadScriptJS($jssFinal);
    $frmUploadJS->setVisible(true);
    
    
}
?>